package level.state;

import java.io.File;
import java.util.ArrayList;

public class StateMemento
{
	private ArrayList<State> states;
	private int position;
	private File file;

	public StateMemento(State state)
	{
		states = new ArrayList<State>();
		states.add(state);
		position = 0;
		file = state.getFile();
	}

	public Boolean isPositionZero()
	{
		return (position == 0 ? true : false);
	}

	public Boolean isPositionEnd()
	{
		return (position == states.size() - 1 ? true : false);
	}

	public void add(State state)
	{
		if (states.size() - position - 1 > 0)
		{
			for (int i = states.size() - 1; i > position; i--)
				states.remove(i);
		}
		states.add(state);
		position++;
		states.get(position).setFile(file);
	}

	public State undo()
	{
		if (!isPositionZero())
			position--;
		states.get(position).setFile(file);
		return states.get(position);
	}

	public State redo()
	{
		if (!isPositionEnd())
			position++;
		states.get(position).setFile(file);
		return states.get(position);
	}

	public void updateFile(File file)
	{
		this.file = file;
	}
}
